Skip to content

feat(agents): 资源库改写 Agent 文件前也先备份,恢复覆盖资源库与供应商的全部修改 - #260

Merged
xiaoyumuxi merged 3 commits into
mainfrom
feat/agent-library-backups
Dec 3, 2026
Merged

xiaoyumuxi merged 3 commits into
mainfrom
feat/agent-library-backups

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

关联

改动

都是 API-45、agents 能力 0 内的补充,不升版本。没有新增错误码、日志事件、宿主方法、通知或插件函数参数,插件后端协议不变,四个 SDK 不需要改。

一、资源库的修改也先备份

  • agentLibrary.apply 改写这三类文件前先备份:
    • 为 MCP 服务器改声明文件;
    • 为 Hooks 改声明文件;
    • 整份写入指令文件,包括用户确认 replace: true 覆盖不是 Lithe 写的内容的那一次。
  • 备份失败就不写文件,复用 agentProviders.backupFailed(语义相同)。
  • 技能是符号链接,不覆盖任何内容,不备份:已有同名项时拒绝链接,取消链接只删指向资源库的链接。
  • 文件写入失败时撤回刚做的备份。

二、一个文件只有一份基线

  • 备份 ID 按路径归并:取第一个解析到该路径的声明文件的 ID;指令文件不是任何声明文件时用 lithe:instructions。
  • 不论资源库还是供应商先改到某个文件,“自上次恢复以来 Lithe 第一次修改前的原样”只存一份,后来的修改不替换它。例如 Codex 的 config.toml 同时有 MCP 服务器和模型供应商。
  • 只由供应商写过的旧记录用的是声明文件 ID,和新规则一致,可以继续用。

三、宿主看到的状态和恢复入口

  • 宿主命令 agentLibrary.read 的每个已授权 Agent,有备份时多一个可选字段 backup {savedAtMs, modifiedSince},和 agentProviders.read 的 backup 完全相同。后者现在也覆盖指令文件和资源库的修改。
  • agentProviders.restore 不再要求 Agent 声明 providers(原来回答 agentProviders.notDeclared),恢复这个 Agent 所有备份过的文件。参数、返回值和其余错误码不变。
  • 没有另加 agentLibrary.restore:备份按 Agent 存,一份备份对应一个恢复入口;另加一个等于同一个操作有两个名字。

四、恢复后状态和磁盘一致

  • 开始写文件之前,Core 先删掉 state.json 里这个 Agent“最后写入的指令哈希”;删不掉就整次恢复失败,一个文件都不写。
  • 恢复后 instructions.state 只按文件实际内容判断,不会显示 written(已是最新);内容和资源库不一样时显示 other,再写入要重新确认 replace: true。
  • 资源库本身、Lithe 保存的供应商列表、插件保存的密钥都不动。

五、文档

  • 契约 shared/plugins/contract/v1/README.md:
    • 资源库目录多了 backups/;agents[].backup;服务器和 Hooks 的写入步骤加“先备份”;技能不备份;指令写入先备份。
    • “Backups and restore”从供应商下面独立成一节,覆盖两类修改、状态、恢复、恢复后的状态和旧 Core 的表现。
    • 给 providers.read 那段补上小标题,它原来误挂在 Backups 标题下。
  • shared/plugins/reference/domains/agents.md 中英各加“修改前备份与恢复”;rust/contracts/rust-core-api.md 三行;API 总表 API-45 描述、能力版本表 agents 0 两行;协议更新记录加一行。

新旧搭配

  • 旧宿主配新 Core:桌面端的 LibraryAgent 没有 backup 字段,会忽略它,资源库页照旧;供应商页的“恢复原配置”会连指令文件和资源库的修改一起恢复;只有资源库的 Agent 在宿主接入前看不到恢复入口。
  • 新宿主配旧 Core:agentLibrary.read 没有 backup,宿主不显示恢复。

桌面端接入在 1lck/Lithe-IDEA#2654 的后续工作包里做,并在那时升级固定的 Core 提交。

已知限制

  • 两个不同的根经由符号链接目录指向同一个文件时,按路径字符串比较会各存一份基线。
  • 旧 Core 写过指令文件(当时不备份)时,新 Core 第一次写入的基线是 Lithe 以前写的文字。恢复后 Core 不再认领它,状态显示 other。
  • 资源库写入也会产生历史条目,每个 Agent 仍最多 20 份。

#249 引入的两个恢复问题(恢复中途存历史失败会留下找不到基线的记录;备份 ID 解析不到路径时恢复永远失败、提示无法清除)不在本 PR 修,已建单 1lck/Lithe-IDEA#2656。

验证(macOS arm64 本机)

命令 结果
cargo test -p lithe-agent-runtime 91 通过,含新增 tests::library::backups 5 个
cargo test --locked --workspace --features lithe-remote/server,lithe-remote/motty,lithe-ide-host/mcp(CI 的 Linux 参数) 2987 通过,0 失败
node scripts/rust-core/verify-platform-tests.mjs 通过(55 个 crate)
node --test scripts/rust-core/*.test.mjs 16 通过
cargo fmt --all -- --check 通过
cargo clippy --locked --workspace --all-targets --all-features -- -D warnings 通过
Windows 目标 clippy(只 -p lithe-agent-runtime -p lithe-core) 通过
node scripts/logging/generate-log-events.mjs --check 通过
scripts/rust-core/verify-rust-core-comments.sh agent-runtime 通过
node shared/plugins/scripts/verify-docs.mjs 通过
新测试逐项计时 5 个,17–173 ms

新增测试覆盖:

  • 第一次写 MCP、Hooks、指令前产生基线;
  • 资源库和供应商先后改同一文件只留一份基线;
  • 备份失败不写文件;
  • 恢复后内容与指令状态一致;
  • modifiedSince 的变化与 discardLaterChanges;
  • replace 覆盖前的内容可以恢复。

测试同时检查日志里没有路径、命令和文本。

没有执行的:

  • 插件后端协议没变,没跑 sdk-e2e.mjs。
  • Windows 上没有实际运行测试;Windows 目标的全工作区 clippy 没跑。
  • “备份成功、写文件失败、撤回备份”这条路径没有自动化测试:可靠地制造写失败只能靠权限位,以 root 运行时不可靠。供应商那边原来也没有这条测试。

- agentLibrary.apply 写 MCP 服务器、Hooks 和整份指令文件(含 replace 覆盖)前先备份,备份失败不写
- 同一个文件不论资源库还是供应商先改,只留一份基线
- agentLibrary.read 的每个 Agent 带 backup {savedAtMs, modifiedSince},与 agentProviders.read 相同
- agentProviders.restore 不再要求声明 providers;放回指令文件前先忘掉 Core 记的指令哈希
- 契约、领域文档、Core API 文档与协议更新记录同步;agents 0 内的补充,不升版本

Refs 1lck/Lithe-IDEA#2654
…ckups

# Conflicts:
#	shared/plugins/reference/api-catalog.md
#	shared/plugins/reference/protocol-changelog.md
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review December 3, 2026 10:31
@xiaoyumuxi
xiaoyumuxi merged commit 362cb81 into main Dec 3, 2026
7 of 9 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 potential issues.

Devin Review

Comment on lines +458 to +468
let instructions = paths
.instructions
.as_deref()
.map(|path| paths.backup_id(path, INSTRUCTIONS_BACKUP_ID));
let _writing = self.lock_writing();
restore(
&config,
&key,
&paths.backed_up(),
request.discard_later_changes,
|restoring| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Restore overwrites a different instruction file

After an agent’s home changes, restore_backup can apply the old home’s baseline to the new instruction file. Writing instructions in the new home updates the same backup’s digest, so restore overwrites the new file without warning.

Learn more

An agent can declare an environment-specific instructions path with a fallback, as in the example agent. resolve_root selects the path from the current environment. apply_instructions keys backups by the agent and instruction ID, not that path. take keeps the first baseline for that ID but refreshes the digest on each write. restore checks the current path against that new digest and then uses the old path’s baseline.

Example: Apply library instructions over “Personal rules” in the default home. Set the CLI home override to a second directory containing “Work rules”, then apply library instructions there. A normal restore writes “Personal rules” to the work directory instead of restoring “Work rules”.

Recommended fix: Persist the resolved target identity with each baseline. Keep separate baselines for separate targets and never restore one into a different target, even with discardLaterChanges. Test both a changed environment root and a switch back to the original root.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +2229 to +2230
let (agent, file) = backup;
let taken = backups::take(config, agent, file, now.as_deref(), &edited)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Restore silently discards intervening settings edits

After a CLI settings edit, edit_staged can carry it into a library write and mark the whole file unchanged. Restore then rolls back the CLI edit without discard confirmation.

Learn more

The plugin edits a copy and keeps unrelated settings, as the configuration edit contract requires. If the same file already has a baseline, take leaves that baseline intact but replaces the recorded digest with the digest of the entire new file. restore compares only that digest before putting the baseline back. Before this PR a library write left the provider backup’s digest unchanged, so restore still required confirmation.

Example: Change the model in Lithe, enable a logging setting in the CLI, then add an MCP server in the library. The MCP edit keeps logging enabled, but restore now accepts the default request and disables logging along with undoing the model and MCP edits.

Recommended fix: Detect divergence from an existing backup’s last-write digest before advancing it. Preserve a separate indication that restoring the baseline would discard outside changes and honor it in both read status and restore until the baseline is retired.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

let agent_key = agent.record_key();
// The same ID a library change uses for this file, so the file keeps
// one baseline whichever kind of change came first.
let backup_id = paths.backup_id(&target_path, &target_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Duplicate baselines break upgraded restores

When two IDs name one file, backup_id can bypass an existing backup under the second ID after upgrade. Restore rejects the conflicting digests or, when forced, can finish with the newer baseline.

Learn more

check_configuration requires distinct IDs, not distinct paths. Before this PR, provider edits recorded the ID returned by the plugin. The new path-based selection always takes the first matching declaration. take treats an unused ID as a new baseline, even if another ID already backs up that path. restore checks and applies both entries in key order, then removes them.

Example: A manifest lists z-main first and a-model second, both naming the same file. Before upgrade, changing model A to B under a-model saves A. After upgrade, changing B to C under the new z-main key saves B. Normal restore rejects the old digest for B; forced restore writes A and then B, and consumes both baselines.

Recommended fix: Look up existing backup records by resolved target before assigning the first declaration’s ID to a new write. If multiple records already refer to one target, reconcile them before any restore write; restore a target at most once and retain the earliest valid baseline.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +2229 to +2230
let (agent, file) = backup;
let taken = backups::take(config, agent, file, now.as_deref(), &edited)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 CLI edits lost during backup

If the CLI saves settings during backups::take, edit_staged can overwrite them without a fresh file check. The backup holds the earlier bytes, so it cannot recover that save.

Learn more

edit_staged checks that the original snapshot still matches before it takes the new backup. Its mutex serializes only Lithe’s library commands. take snapshots those already-read bytes and synchronously writes backup data and the record. An independent CLI can save after the check, and the final rename replaces that save. apply_instructions adds the same backup window between reading the file and replacing it.

Example: Turn on an MCP server in Lithe while the CLI saves a preference. If the CLI save completes during backup sync, the staged file wins and neither the baseline nor the history for that write contains the preference.

Recommended fix: Revalidate the target and original bytes after the backup and as close to the rename as possible, undoing the pending backup on a mismatch. Apply the same check to instructions. A strict guarantee against arbitrary external writers requires a conditional replacement or a lock those writers honor.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant